﻿using System;
using System.Reflection;
using NUnit.Core;
using NUnit.Framework;
using TestMethod=SqlTdd.Core.Model.SqlTddTestMethod;

namespace SqlTdd.Addin
{
    public class TestMethodBuilder : NUnitTestMethod
    {
        private readonly string _connectionString;
        private readonly TestMethod _testMethod;

        public TestMethodBuilder(MethodInfo templateMethod, TestMethod testMethod, string connectionString)
            : base(templateMethod)
        {
            TestName.Name = testMethod.Name;
            TestName.FullName = testMethod.Name;
            _connectionString = connectionString;
            _testMethod = testMethod;
        }

        public override void Run(TestCaseResult testResult)
        {
            try
            {
                base.Run(testResult);
                _testMethod.Run(_connectionString);
            }
            catch (AssertionException ex)
            {
                testResult.Failure("SqlTdd TestMethod Failure:" + ex.Message, ex.StackTrace);
            }
            catch (Exception ex)
            {
                testResult.Error(ex);
            }
        }
    }
}